<?php
namespace app\adminapiv3\service;

use app\common\service;
class HostModuleRelation extends Base
{
    /**
     * @name: 未添加主机列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-02 14:58:01
     */
    function notAddedHostList ($module,  $filterData, $limit)
    {
        $Host = new service\Host();
        $HostModuleRelation = new service\HostModuleRelation();
        $module = $HostModuleRelation->toDo('fieldCheck', [$module]);
        $ServerAuth = new service\ServerAuth();
        $remianingNum = $ServerAuth->toDo('remainingNum', [$module]);
        $return = $Host->toDo('notAddedModule', [$module, $filterData, $limit]);
        $return['remianing_number'] = $remianingNum;
        
        return $return;
    }

    /**
     * @name: 添加主机
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-02 15:10:25
     */
    function addRelation ($module)
    {
        $host_id_a = $this->getHost('id');
        $HostModuleRelation = new service\HostModuleRelation();
        $HostLink = new service\HostLink;
        $module = $HostModuleRelation->toDo('fieldCheck', [$module]);//检测主机是否有效
        $HostModuleRelation->toDo('checkStatus', [$module, 1]);//检测是否已开启

        $ServerAuth = new service\ServerAuth();
        $ServerAuth->toDo('checkRemaining', [$module, 2]);//检测是剩余授权

        $host_id_os = $this->getHost('os');

        $HostModuleRelation->toDo('checkConflict', [$module]);//检测是否冲突
        $HostModuleRelation->toDo('checkRelyOn', [$module, $host_id_os]);//检测是否依赖


        $HostModuleRelation->toDo('add', [$host_id_a, $module]);//添加模块关系
        $HostLink->toDo('add', [$module, $host_id_a]);//添加模块在线状态
        $this->toDo('add_'.$module);//模块初始化
    }

    /**
     * @name: 从模块中删除主机
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-02 15:14:30
     */
    function delRelation ($module)
    {
        $host_id_a = $this->getHost('id');
        $HostModuleRelation = new service\HostModuleRelation();
        $HostLink = new service\HostLink();
        $HostLink->toDo('offline', [$host_id_a, $module]);//主动离线
        $module = $HostModuleRelation->toDo('fieldCheck', [$module]);
        $HostModuleRelation->toDo('checkStatus', [$module, 0]);//检测是否已关闭
        $HostModuleRelation->toDo('del', [$host_id_a, $module]);

        $this->toDo('del_'.$module);//状态重置
    }

    /**
     * @name: netfilter服务
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-09 16:27:38
     */
    function add_netfilter ()
    {
        $host_id_a = $this->getHost();
        $HostLearnConfig = new service\HostLearnConfig;
        $HostAccessLogConfig = new service\HostAccessLogConfig();
        $NetfilterSafeRule = new service\NetfilterSafeRule();
        $ServerProxy = new service\ServerProxy();
        $proxyData = $ServerProxy->allProxy();//查询所有代理服务器

        foreach ($host_id_a as $host_id => $info)
        {
            $use_rule = $HostLearnConfig->toDo('add', [$host_id, $info['osversion']]);//学习netfilter 配置
            $HostAccessLogConfig->toDo('add', [$host_id]);//添加netfilter访问日志
            $NetfilterSafeRule->toDo('add', [$host_id, $proxyData, $use_rule]);//添加默认规则
        }
        
    }

    /**
     * @name: hgshield 服务
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-09 16:28:14
     */
    function add_hgshield ()
    {
        $host_id_a = $this->getHost('id');

        $HNRGR = new service\HostNormalRuleGroupRelation();
        $HNRR = new service\HostNormalRuleRelation();
        $IpsConfig = new service\IpsConfig();
        $hostServer = new service\Host();
        foreach ($host_id_a as $host_id)
        {
            $HNRGR->add($host_id);
            $HNRR->add($host_id);
            $ipsConfigData['host_id'] = $host_id;
            $ipsConfigData['protect_ip'] = $hostServer->GetHostIp($host_id);
            $IpsConfig->add($ipsConfigData);
        }
        
    }

    /**
     * @name: 端口伪装
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-09 16:28:52
     */
    function add_disguise ()
    {
        $host_id_a = $this->getHost('os');
        $HostGuisePort = new service\HostGuisePort();
        $HostGuiseConfig = new service\HostGuiseConfig();
        foreach ($host_id_a as $host_id => $os)
        {
            $HostGuisePort->addDefaultPort($host_id, $os);
            $HostGuiseConfig->add($host_id);
        }
    }

    /**
     * @name: 删除netfilter 模块
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-24 11:22:24
     */
    function del_netfilter ()
    {
        $host_id_a = $this->getHost('id');
        $HostLearnConfig = new service\HostLearnConfig();
        $HostLearnConfig->learnOff($host_id_a);//关闭学习
    }

    /**
     * @name: 删除hgshield
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-24 13:16:36
     */
    function del_hgshield ()
    {

    }

    /**
     * @name: 删除伪装端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-24 13:24:42
     */
    function del_disguise()
    {
        $host_id_a = $this->getHost('id');
        $HostGuiseConfig = new service\HostGuiseConfig();
        $HostGuiseConfig->onOff($host_id_a, 0);
    }
}